﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using H2.NET.Common;
using Ultimus.WFServer;

namespace H2.NET.Web.Workflow.Tools
{
    /// <summary>
    /// 生成流程监控图
    /// </summary>
    public class FlowChart : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var flag = false;
                var strTaskId = context.Request.QueryString["TaskID"];

                //数据合法性验证
                if (strTaskId == null || strTaskId.Trim() == "")
                {
                    TaskDB.Log("", false, "调用 FlowChart.ashx ProcessRequest 事件出错：TaskID 传入不正确！");
                    return;
                }

                //根据 TaskID 去取得表单对应的 url 地址
                var tskTemp = new Task();
                flag = tskTemp.InitializeFromTaskId(strTaskId);

                //如果加载成功时
                if (flag)
                {
                    byte[] bytesGif;
                    var incsTemp = new Incident.Status();

                    //生成监控图像
                    incsTemp.GetGraphicalStatus(tskTemp.strProcessName, tskTemp.nIncidentNo, tskTemp.nProcessVersion, out bytesGif);
                    context.Response.ContentType = "image/gif";
                    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    context.Response.BinaryWrite(bytesGif);
                }
                else
                {
                    TaskDB.Log("", false, "调用 FlowChart.ashx ProcessRequest 事件出错：TaskID: " + strTaskId + " 传入不正确，无法取得对应的任务信息！");
                    return;
                }

            }
            catch (Exception ee)
            {
                TaskDB.Log("", false, "调用 FlowChart.ashx ProcessRequest 事件出错：" + ee.Message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}